define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'newhouse/newhouse_panorama/index' + location.search,
                    add_url: 'newhouse/newhouse_panorama/add',
                    edit_url: 'newhouse/newhouse_panorama/edit',
                    del_url: 'newhouse/newhouse_panorama/del',
                    multi_url: 'newhouse/newhouse_panorama/multi',
                    import_url: 'newhouse/newhouse_panorama/import',
                    table: 'newhouse_panorama',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'lx', title: __('Lx'), searchList: {"0":__('Lx 0'),"1":__('Lx 1')}, formatter: Table.api.formatter.normal},
                        {field: 'category.name', title: __('Category_id'), formatter: Table.api.formatter.normal},
                        {field: 'url1', title: __('Url1'), formatter: Table.api.formatter.url},
                        {field: 'pic', title: __('Pic'), formatter: Table.api.formatter.image, events: Table.api.events.image},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'admin.nickname', title: __('Admin_id')},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD HH:mm"},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

            // 标签selectPage下拉选择器
            $("#c-newhouse_layout_ids").data("params", function (obj) {
                var newhouse_id = $('#c-newhouse_ids').val()
                // console.log(category)
                if(newhouse_id == 0){
                    // console.log('空值')
                    // 没有数据flag=2 所以获取返回的是空 显示无结果
                    return {custom: {flag:2}}
                }
                // return {custom: {type: $("#c-type").val()}};
                return {custom: {flag:1},custom: {newhouse_id: newhouse_id}};
            });

            var v = $('#c-category_id').val()
            // 一进来就改变样式
            if(v == 39 | v == '户型赏析'){
                $('.c-newhouse_ids').attr('data-multiple','false')
                $('.newhouse_layout_class').attr('style','display:block;')
                $('.fovi_class').attr('style','display:none;')
            }else if(v == 43 | v == '项目实景'){
                $('.c-newhouse_ids').attr('data-multiple','false')
                $('.newhouse_layout_class').attr('style','display:none;')
                $('.fovi_class').attr('style','display:none;')
            }else if(v == 44 | v == '鸟瞰湛江'){
                $('.c-newhouse_ids').attr('data-multiple','true')
                $('.newhouse_layout_class').attr('style','display:none;')
                $('.fovi_class').attr('style','display:true;')
            }
            // 值有改变后执行
            $('#c-category_id').change(function(){
                var v = $(this).val();
                if(v == 39 | v == '户型赏析'){
                    $('.c-newhouse_ids').attr('data-multiple','false')
                    $('.newhouse_layout_class').attr('style','display:block;')
                    $('.fovi_class').attr('style','display:none;')
                    console.log(v)
                }else if(v == 43 | v == '项目实景'){
                    $('.c-newhouse_ids').attr('data-multiple','false')
                    $('.newhouse_layout_class').attr('style','display:none;')
                    $('.fovi_class').attr('style','display:none;')
                    console.log(v)
                }else if(v == 44 | v == '鸟瞰湛江'){
                    $('.c-newhouse_ids').attr('data-multiple','true')
                    $('.newhouse_layout_class').attr('style','display:none;')
                    $('.fovi_class').attr('style','display:true;')
                    console.log(v)
                }
            })
        },
        edit: function () {
            Controller.api.bindevent();

            // 标签selectPage下拉选择器
            $("#c-newhouse_layout_ids").data("params", function (obj) {
                var newhouse_id = $('#c-newhouse_ids').val()
                // console.log(category)
                if(newhouse_id == 0){
                    // console.log('空值')
                    // 没有数据flag=2 所以获取返回的是空 显示无结果
                    return {custom: {flag:2}}
                }
                // return {custom: {type: $("#c-type").val()}};
                return {custom: {flag:1},custom: {newhouse_id: newhouse_id}};
            });

            var v = $('#c-category_id').val()
            // 一进来就改变样式
            if(v == 39 | v == '户型赏析'){
                $('.c-newhouse_ids').attr('data-multiple','false')
                $('.newhouse_layout_class').attr('style','display:block;')
                $('.fovi_class').attr('style','display:none;')
            }else if(v == 43 | v == '项目实景'){
                $('.c-newhouse_ids').attr('data-multiple','false')
                $('.newhouse_layout_class').attr('style','display:none;')
                $('.fovi_class').attr('style','display:none;')
            }else if(v == 44 | v == '鸟瞰湛江'){
                $('.c-newhouse_ids').attr('data-multiple','true')
                $('.newhouse_layout_class').attr('style','display:none;')
                $('.fovi_class').attr('style','display:true;')
            }
            // 值有改变后执行
            $('#c-category_id').change(function(){
                var v = $(this).val();
                if(v == 39 | v == '户型赏析'){
                    $('.c-newhouse_ids').attr('data-multiple','false')
                    $('.newhouse_layout_class').attr('style','display:block;')
                    $('.fovi_class').attr('style','display:none;')
                }else if(v == 43 | v == '项目实景'){
                    $('.c-newhouse_ids').attr('data-multiple','false')
                    $('.newhouse_layout_class').attr('style','display:none;')
                    $('.fovi_class').attr('style','display:none;')
                }else if(v == 44 | v == '鸟瞰湛江'){
                    $('.c-newhouse_ids').attr('data-multiple','true')
                    $('.newhouse_layout_class').attr('style','display:none;')
                    $('.fovi_class').attr('style','display:true;')
                }
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                var input_id = $('#plupload-image').data("input-id") ? $('#plupload-image').data("input-id") : "";
                var preview_id = $('#plupload-pic').data("preview-id") ? $('#plupload-pic').data("preview-id") : "";

                if(preview_id) {
                    $("#plupload-pic").data("upload-success", function(data, ret) {
                        //这里进行后续操作
                        // console.log(data)
                        // console.log(ret)
                        var imageCount = $('#p-pic').children('li').length
                        // console.log($imageCount)
                        // var newsType = $('#c-type').val();
                        if(imageCount == 1) {
                            $(this).hide();
                        }
                        // if(newsType == 0){
                        //     if(imageCount == 3) {
                        //         $(this).hide();
                        //     }
                        // } else {
                        //     $(this).hide();
                        // }
                    });
                    // 移除按钮事件
                    $(document.body).off("click", "#" + preview_id + " .btn-trash");
                    // 再绑定新按钮事件
                    $(document.body).on("click", "#" + preview_id + " .btn-trash", function () {
                        var that = $(this);
                        // 用这个选择有毒 必须要先移除按钮事件。
                        var path = $("#" + preview_id + " [data-url]");
                        // 改成选择当前a标签的上一个元素获取path
                        // var path = that.prev().attr('data-url');
                        // console.log(that.prev().attr('data-url'))
                        Layer.confirm('是否删除该图片？（删除后不可恢复）',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (index) {
                                var options = {
                                    url: 'newhouse/newhouse_panorama/delCover',
                                    data: {
                                        id: $("#plupload-pic").data('pic-id'),
                                        path: path.data('url')
                                    }
                                }
                                Fast.api.ajax(options, function () {
                                    if (input_id) {
                                        $("#" + input_id).val('');
                                    }
                                    that.closest("li").remove();
                                    $("#plupload-pic").show();
                                    Layer.close(index);
                                });
                            }
                        );
                    });

                }
            }
        }
    };
    return Controller;
});
